home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / games / ted5.zip / XMS.C < prev    next >
C/C++ Source or Header  |  1993-02-04  |  5KB  |  200 lines

  1. ////////////////////////////////////////////////////
  2. //
  3. // XMS routines - uses the MENU.C & LIB.C routines for output
  4. //
  5. ////////////////////////////////////////////////////
  6. #include "ted5.h"
  7. #pragma hdrstop
  8.  
  9.  
  10. long TTLxms;
  11. unsigned XMSavail;
  12. void far *XMSdriver;
  13.  
  14. ////////////////////////////////////////////////////
  15. //
  16. // CALL the XMSdriver with a function #
  17. //
  18. ////////////////////////////////////////////////////
  19. void CallXMS(char func)
  20. {
  21.  asm    mov    ah,[func]
  22.  asm    call    [DWORD PTR XMSdriver]
  23. }
  24.  
  25. ////////////////////////////////////////////////////
  26. //
  27. // Initialize the XMS memory
  28. //
  29. ////////////////////////////////////////////////////
  30. void InitXMS(void)
  31. {
  32.  //
  33.  // See if XMS driver is present...
  34.  //
  35.  asm    mov    ax,0x4300
  36.  asm    int    0x2f
  37.  asm    cmp    al,0x80        // installed?
  38.  asm    je    Present
  39.  
  40.  ErrDialog("You poor bastard! I refuse\n"
  41.        "to work without Extended\n"
  42.        "Memory! Goodbye!"," I Suck ");
  43.  Quit("No Extended Memory Driver found!");
  44.  
  45.  //
  46.  // YES! We found an XMS driver! Now we
  47.  // need to get the XMS driver's entry address...
  48.  //
  49.  Present:
  50.  asm    mov    ax,0x4310
  51.  asm    int    0x2f
  52.  asm    mov    WORD PTR XMSdriver,bx
  53.  asm    mov    WORD PTR XMSdriver+2,es
  54.  
  55.  XMSTotalFree();
  56. }
  57.  
  58.  
  59. ////////////////////////////////////////////////////
  60. //
  61. // Report an XMS error, if any
  62. //
  63. ////////////////////////////////////////////////////
  64. void XMSerror(void)
  65. {
  66.  if (_AX==0)
  67.    switch(_BL)
  68.    {
  69.     case 0x80: Quit("Function not implemented!");
  70.     case 0x81: Quit("VDISK device driver was detected!");
  71.     case 0x82: Quit("A20 error occurred!");
  72.     case 0x8e: Quit("General driver error!");
  73.     case 0x8f: Quit("Unrecoverable driver error!");
  74.  
  75.     case 0x90: Quit("High memory area does not exist!");
  76.     case 0x91: Quit("High memory area already in use!");
  77.     case 0x92: Quit("DX is less than /HMAMIN= parameter!");
  78.     case 0x93: Quit("High memory area not allocated!");
  79.     case 0x94: Quit("A20 line still enabled!");
  80.  
  81.     case 0xa0: Quit("Not enough Extended Memory available!");
  82.     case 0xa1: Quit("Extended memory handles exhausted!");
  83.     case 0xa2: Quit("Invalid handle!");
  84.     case 0xa3: Quit("Invalid source handle!");
  85.     case 0xa4: Quit("Invalid source offset!");
  86.     case 0xa5: Quit("Invalid destination handle!");
  87.     case 0xa6: Quit("Invalid destination offset!");
  88.     case 0xa7: Quit("Invalid length!");
  89.     case 0xa8: Quit("Invalid overlap in move request!");
  90.     case 0xa9: Quit("Parity error detected!");
  91.     case 0xaa: Quit("Block is not locked!");
  92.     case 0xab: Quit("Block is locked!");
  93.     case 0xac: Quit("Lock count overflowed!");
  94.     case 0xad: Quit("Lock failed!");
  95.  
  96.     case 0xb0: Quit("Smaller UMB is available!");
  97.     case 0xb1: Quit("No UMBs are available!");
  98.     case 0xb2: Quit("Invalid UMB segment number!");
  99.  
  100.     default: Quit("Unknown XMS Memory Error! I'm totally stumped!");
  101.    }
  102. }
  103.  
  104. ////////////////////////////////////////////////////
  105. //
  106. // Allocate XMS memory
  107. //
  108. ////////////////////////////////////////////////////
  109. int XMSAllocate(long size)
  110. {
  111.  #if 0
  112.  static int allocnum=0;
  113.  
  114.  _MouseHide();
  115.  sx=0;
  116.  sy=1;
  117.  print("XMS allocation #");
  118.  printint(allocnum++);
  119.  print(":");
  120.  printint((1023L+size)/1024L);
  121.  print("K");
  122.  while(!keydown[0x1c]);
  123.  clearkeys();
  124.  _MouseShow();
  125.  #endif
  126.  
  127.  
  128.  _DX=(size+1023)/1024;
  129.  CallXMS(9);
  130.  XMSerror();
  131.  asm push dx
  132.  TTLxms=1024L*XMSTotalFree();
  133.  asm pop dx
  134.  return _DX;
  135. }
  136.  
  137. ////////////////////////////////////////////////////
  138. //
  139. // Free XMS memory
  140. //
  141. ////////////////////////////////////////////////////
  142. void XMSFreeMem(int handle)
  143. {
  144.  _DX=handle;
  145.  CallXMS(10);
  146.  XMSerror();
  147.  TTLxms=1024L*XMSTotalFree();
  148. }
  149.  
  150.  
  151. ////////////////////////////////////////////////////
  152. //
  153. // Return XMS memory available
  154. //
  155. ////////////////////////////////////////////////////
  156. unsigned XMSTotalFree(void)
  157. {
  158.  CallXMS(8);
  159.  XMSerror();
  160.  XMSavail=_DX;
  161.  return XMSavail;
  162. }
  163.  
  164. ////////////////////////////////////////////////////
  165. //
  166. // Move XMS memory
  167. //
  168. ////////////////////////////////////////////////////
  169. void XMSmove(int srchandle,long srcoff,int desthandle,long destoff,long size)
  170. {
  171.  struct { long bsize;
  172.       int shandle;
  173.       long soff;
  174.       int dhandle;
  175.       long doff;
  176.     } XMSparms;
  177.  
  178.  unsigned DSreg,SIreg;
  179.  
  180.  XMSparms.bsize=size+(size&1);
  181.  XMSparms.shandle=srchandle;
  182.  XMSparms.dhandle=desthandle;
  183.  XMSparms.soff=srcoff;
  184.  XMSparms.doff=destoff;
  185.  
  186.  DSreg=FP_SEG(&XMSparms);
  187.  SIreg=FP_OFF(&XMSparms);
  188.  
  189.  asm    push    ds
  190.  asm    push    si
  191.  asm    mov    si,SIreg
  192.  asm    mov    ds,DSreg
  193.  
  194.  CallXMS(11);
  195.  
  196.  asm    pop    si
  197.  asm    pop    ds
  198.  
  199.  XMSerror();
  200. }